import { defineStore } from 'pinia'
import { ref } from 'vue'

const initState = { nickname: '', avatar: '' }

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref<IUserInfo>({ ...initState })
    const token = ref('')

    const setUserInfo = (val: IUserInfo) => {
      userInfo.value = val
    }
    const setToken = (val: string) => {
      token.value = val
    }

    const clearUserInfo = () => {
      userInfo.value = { ...initState }
    }
    const clearToken = () => {
      token.value = ''
    }

    // 是否登录
    const isLogined = computed(() => !!token.value)

    return {
      userInfo,
      setUserInfo,
      setToken,
      clearUserInfo,
      clearToken,
      isLogined,
    }
  },
  {
    persist: true,
  },
)
